//
//  SupViewController.m
//  家范
//
//  Created by 吴军虎吴军虎 on 16/4/21.
//  Copyright © 2016年 lanjue. All rights reserved.
//

#import "SupViewController.h"
#import "YSLDraggableCardContainer.h"
#import "SupView.h"

@interface SupViewController ()<YSLDraggableCardContainerDataSource, YSLDraggableCardContainerDelegate>

@property(nonatomic,retain)UIView *myView;
@property(nonatomic,retain)NSArray *myAtrray;
@property (nonatomic, strong) YSLDraggableCardContainer *container;
@property (nonatomic, strong) UIImageView *imageViewL;
@property (nonatomic, strong) UIImageView *imageViewR;


@end

@implementation SupViewController
{
    NSInteger i;
}

-(void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden=YES;
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden=NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.myAtrray = [NSArray arrayWithObjects:@"touxiangImage1.jpg",@"touxiangImage3.jpg",@"touxiangImage4.jpg",@"touxiangImage2.jpg", nil];
    i = 0;
    
    _container = [[YSLDraggableCardContainer alloc]init];
    _container.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _container.backgroundColor = [UIColor clearColor];
    _container.dataSource = self;
    _container.delegate = self;
    _container.canDraggableDirection = YSLDraggableDirectionLeft | YSLDraggableDirectionRight;
    [self.view addSubview:_container];
    
    //左侧图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fahui"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    //每日速配
    self.title = @"每日速配";
    self.view.backgroundColor = LV;
    
#pragma mark 按钮可以封装
    //左边图片按钮
    UIButton *buttonL=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonL setBackgroundImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
    [_container addSubview:buttonL];
    buttonL.tag = 1011;
    [buttonL addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    [buttonL mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.bottom.mas_equalTo(-125 *AUTO_WIDTH);
        make.left.equalTo(self.view).offset(45/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(120 *AUTO_WIDTH, 44 *AUTO_WIDTH));
    }];
    
    
    self.imageViewL = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiaa"]];
    [buttonL addSubview:_imageViewL];
    self.imageViewL.alpha = 0;
    self.imageViewL.userInteractionEnabled = YES;
    [self.imageViewL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.top.bottom.mas_equalTo(@0);
    }];
    
    //右边图片按钮
    UIButton *buttonR=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonR setBackgroundImage:[UIImage imageNamed:@"cang"] forState:UIControlStateNormal];
    [_container addSubview:buttonR];
    buttonR.tag = 1010;
    [buttonR addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
    [buttonR mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.top.equalTo(buttonL.mas_top);
        make.right.mas_equalTo(-45/2 *AUTO_WIDTH);
        make.size.mas_equalTo(CGSizeMake(120 *AUTO_WIDTH, 44 *AUTO_WIDTH));
    }];
    
    
    self.imageViewR = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cangg"]];
    [buttonR addSubview:_imageViewR];
    self.imageViewR.alpha = 0;
    self.imageViewR.userInteractionEnabled = YES;
    [self.imageViewR mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.top.bottom.mas_equalTo(@0);
    }];
    
    
    //圆形按钮
    UIImageView *imageH=[[UIImageView alloc]init];
    imageH.image=[UIImage imageNamed:@"supei_centerC"];
    imageH.userInteractionEnabled=YES;
    UITapGestureRecognizer *imagepp=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageClick:)];
    [imageH addGestureRecognizer:imagepp];
    [_container addSubview:imageH];
    [imageH mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.top.equalTo(buttonL);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(50 *AUTO_WIDTH, 50 *AUTO_WIDTH));
    }];
    
    [_container reloadCardContainer];
    
}

-(void)Back:(UIButton *)click{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)tapAction{
    //    通过改变view的transform属性值中的z轴坐标来实现旋转(x上下翻转,y轴左右翻转)
    CABasicAnimation *basAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basAnimation.duration = 5;
    basAnimation.fromValue = [NSNumber numberWithInt:0];
    basAnimation.toValue = [NSNumber numberWithInt:M_PI*2];
    [basAnimation setRepeatCount:NSIntegerMin];
    [basAnimation setAutoreverses:YES];//逆向回到原来的位置
    [basAnimation setCumulative:NO];//叠加动画效果
    //    [_TXimage.layer addAnimation:basAnimation forKey:@"basicAnimation"];
    
}

#pragma mark -- Selector
- (void)buttonTap:(UIButton *)button
{
    if (button.tag == 1010) {
        [_container movePositionWithDirection:YSLDraggableDirectionRight isAutomatic:YES];
    }
    else if (button.tag == 1011) {
        [_container movePositionWithDirection:YSLDraggableDirectionLeft isAutomatic:YES];
    }
}

- (void)imageClick:(UITapGestureRecognizer *)tap {
    
    NSLog(@"圆形按钮被点击了");
}


#pragma mark - YSLDraggableCardContainer DataSource
- (UIView *)cardContainerViewNextViewWithIndex:(NSInteger)index
{
    SupView *view = [[SupView alloc]initWithFrame:CGRectMake(45.00 / 2 * AUTO_WIDTH, 70.00 /2 * AUTO_WIDTH, SCREEN_WIDTH - (45 * AUTO_WIDTH), 371.00 / 2 * AUTO_WIDTH + 478.00 / 2 * AUTO_WIDTH)];
    
    view.backgroundColor = [UIColor whiteColor];
    view.layer.masksToBounds=YES;
    view.layer.borderColor = KH.CGColor;
    view.layer.borderWidth = 0.4;
    view.layer.cornerRadius=10;
    return view;
}

- (NSInteger)cardContainerViewNumberOfViewInIndex:(NSInteger)index
{
    return 10;
}

#pragma mark -- YSLDraggableCardContainer Delegate
- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didEndDraggingAtIndex:(NSInteger)index draggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection
{
    if (draggableDirection == YSLDraggableDirectionLeft) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
}

- (void)cardContainderView:(YSLDraggableCardContainer *)cardContainderView updatePositionWithDraggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio
{
    SupView *view = (SupView *)draggableView;
    
    if (draggableDirection == YSLDraggableDirectionDefault) {
        view.topHeadView.alpha = 0;
        view.TXimage.alpha = 1;
        _imageViewL.alpha = 0;
        _imageViewR.alpha = 0;
        view.layer.borderWidth = 0.5;
    }
    
    if (draggableDirection == YSLDraggableDirectionLeft) {
        view.topHeadView.image = [UIImage imageNamed:@"next"];
        view.topHeadView.alpha = widthRatio > 0.8 ? 0.8 : widthRatio;
        view.TXimage.alpha = (1 - widthRatio) < 0.2 ? 0.2 : (1 - widthRatio);
        _imageViewL.alpha = widthRatio > 1 ? 1 : widthRatio;
        view.layer.borderWidth = 0;
        
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
        view.topHeadView.image = [UIImage imageNamed:@"you"];
        view.topHeadView.alpha = widthRatio > 0.8 ? 0.8 : widthRatio;
        view.TXimage.alpha = (1 - widthRatio) < 0.2 ? 0.2 : (1 - widthRatio);
        _imageViewR.alpha = widthRatio > 1 ? 1 : widthRatio;
        view.layer.borderWidth = 0;
    }
}

- (void)cardContainerViewDidCompleteAll:(YSLDraggableCardContainer *)container;
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [container removeCardContainer];
        [container reloadCardContainer];
    });
}

- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didSelectAtIndex:(NSInteger)index draggableView:(UIView *)draggableView
{
    NSLog(@"++ index : %ld",(long)index);
}



@end
